/*
 * @Author: 姚潘涛
 * @Date: 2024-04-08 15:18:03
 * @LastEditors: 姚潘涛
 * @LastEditTime: 2024-10-10 15:14:02
 * @Description:
 *
 * Copyright (c) 2024 by pandaman, All Rights Reserved.
 */
#pragma once

#include "xict_common/log/log.h"
#include "xict_cpp/core/pipeline/base.h"
#include "xict_cpp/core/stage/canet_processor.h"
#include "xict_cpp/core/stage/draw_trajectory_processor.h"
#include "xict_cpp/core/stage/heartbeat_sender.h"
#include "xict_cpp/core/stage/stream_processor.h"
#include <thread>

namespace xict_cpp {
    namespace core {
        namespace pipeline {
            struct PipelineControlParams {
                /// @brief The switcher of process video stream;
                bool enable_stream = false;
                /// @brief The switcher of process can data;
                bool enable_can = false;
                /// @brief The switcher of send heartbeat;
                bool enable_heartbeat = false;
                /// @brief The switcher of draw trajectory;
                bool enable_draw_trajectory = false;
                /// @brief The switcher of start ui thread;
                bool enable_ui_thread = true;
                /// @brief The switcher of start unity thread;
                bool enable_unity_thread = true;
            };

            struct RemoteControllerPipelineParams {
                /// @brief The parameters of the pipeline control stage.
                PipelineControlParams switcher;
                /// @brief The parameters of the stream processor stage.
                xict_cpp::core::stage::StreamProcessorStageParams stream;
                /// @brief The parameters of the can preprocessor stage.
                xict_cpp::core::stage::CanetProcessorStageParams canet;
                /// @brief The parameters of the heartbeat sender stage.
                xict_cpp::core::stage::HeartbeatSenderStageParams heartbeat;
                /// @brief The parameters of the draw line stage.
                std::vector<xict_cpp::core::stage::DrawTrajectoryStageParams>
                    trajectories_params_list;
                /// @brief The path of the scripts.
                xict_cpp::core::stage::ScriptsPath scripts_path;
            };

            class RemoteControllerPipeline : public Pipeline {
            public:
                /// @brief Default constructor.
                RemoteControllerPipeline() = default;
                /// @brief Default destructor.
                ~RemoteControllerPipeline() override;

            public:
                /// @brief Initialize the pipeline with the default parameters.
                /// @return True if the initialization is successful, false
                /// otherwise.
                bool Init() override;
                /// @brief Initialize the pipeline with the given parameters.
                /// @return True if the initialization is successful, false
                /// otherwise.
                bool Init(const RemoteControllerPipelineParams& params);
                /// @brief Start the pipeline.
                void Start() override;
                /// @brief Stop the pipeline.
                void Stop() override;
                /// @brief Get the name of the pipeline.
                /// @return The name of the pipeline.
                std::string Name() const override;

            protected:
                /// @brief Initialize the pipeline.
                bool Initialize() override;
                /// @brief Internal process function for the pipeline.
                bool InnerProcess() override;
                /// @brief Reset the pipeline.
                void Reset() override;
                /// @brief Run the stream process pipeline.
                void RunStreamProcessor();
                /// @brief Run the canet process pipeline.
                void RunCanetProcessor();
                /// @brief Run the heartbeat process pipeline.
                void RunHeartbeatSender();
                /// @brief Run the draw trajectory process pipeline.
                void RunDrawTrajectoryProcessor();

            protected:
                /// @brief The parameters of the pipeline.
                RemoteControllerPipelineParams params_;
                /// @brief The flag to start the pipeline.
                bool stop_flag_ = false;
                /// @brief The thread to run the pipeline.
                std::vector<std::thread> threads_;
                /// @brief The stream processor stage.
                std::shared_ptr<xict_cpp::core::stage::StreamProcessorStage>
                    stream_processor_stage_;
                /// @brief The canet processor stage.
                std::shared_ptr<xict_cpp::core::stage::CanetProcessorStage>
                    canet_processor_stage_;
                /// @brief The heartbeat sender stage.
                std::shared_ptr<xict_cpp::core::stage::HeartbeatSenderStage>
                    heartbeat_sender_stage_;
                /// @brief The draw trajectory processor stage.
                std::shared_ptr<xict_cpp::core::stage::DrawTrajectoryStage>
                    draw_trajectory_processor_stage_;
            };
        }   // namespace pipeline
    }       // namespace core
}   // namespace xict_cpp
